/*
 * - Author: 
 * - License: GNU Lesser General Public License (GPL)
 */
package com.springmvc.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.springmvc.quantity.Global;
import com.springmvc.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 分页参数分装
 *
 */
public class PageDto {
	
	private Integer id;
	
	/**当前页**/
	private int page;
	
	/**每页显示总记录数**/
	private int limit;

	/**每页的开始记录数**/
	private int start;
	
	/**排序字段**/
	private String sortColumn;
	
	/**排序方式**/
	private String sortType;

    protected long totalCount = -1L;

    protected long totalSize = 0;

    private String tableName;

	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public int getStart() {
		this.start = (page-1)*limit;
		return start;
	}
    public long getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }
    public long getTotalSize() {
        if (this.totalCount < 0L) {
            return -1L;
        }

        long count = this.totalCount / this.limit;
        if (this.totalCount % this.limit > 0L) {
            count += 1L;
        }
        return count;
    }
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	/**
	 * 获取数据库名称
	 */
	@JsonIgnore
	public String getDbName(){
		return Global.getConfig("jdbc.type");
	}
	public String getSortColumn() {
		if (!StringUtils.isBlank(sortColumn)) {
			Pattern p=Pattern.compile("[A-Z]");
			StringBuilder builder=new StringBuilder(sortColumn);
		    Matcher mc=p.matcher(sortColumn);
		    int i=0;
		    while(mc.find()){
		        builder.replace(mc.start()+i, mc.end()+i, "_"+mc.group().toLowerCase());
		        i++;
		    }
		    if('_' == builder.charAt(0)){
		        builder.deleteCharAt(0);
		    }
		    return builder.toString();
		}
		return sortColumn;
	}
	public void setSortColumn(String sortColumn) {
		this.sortColumn = sortColumn;
	}
	public String getSortType() {
		return sortType;
	}
	public void setSortType(String sortType) {
		this.sortType = sortType;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
}
